草庐IT

Java final 与 C++ const

全部标签

c++ - 这是什么意思? : note: no known conversion for argument 1 from ‘int’ to ‘const account&’

我试图理解我们通常在C++程序中遇到的错误的含义。在编译程序时我遇到了一个错误(我故意犯了这个错误,请不要告诉我如何更正它)并且存在一个注释:note:noknownconversionforargument1from‘int’to‘constaccount&’我想看懂这张纸条的意思。我的程序是:#includeclassaccount{private:inta_no;public:account(){a_no=0;}voidshowData(){std::cout我知道我还没有定义一个可以接受一个参数的构造函数,这样做会消除我的错误。好的,编译时我得到了:file1.cpp:Infu

c++ - 将 unique_ptr<Object> 作为 unique_ptr<const Object> 返回

我有这样的方法:std::unique_ptrTable::GetStats()const{std::unique_ptrresult;//...//Preparestats.Undersomeconditionsanexceptionmaybethrown.//...returnresult;}问题是它无法编译:error:cannotbind‘std::unique_ptr’lvalueto‘std::unique_ptr&&’我可以使用以下绕过方法使其编译:returnstd::unique_ptr(result.release());不过好像有点过分了。我无法理解,从C++的角

c++ - propagate_const 和前向声明

我刚刚遇到了奇怪的std::experimental::propagate_const错误。以下片段演示了问题#include#include#includeclassFWD;//compilesclassA{std::unique_ptrm;};//compilesclassB{std::experimental::propagate_const>m;};//compilesclassC{std::unique_ptr>m;};//doesnotcompile!classD{std::experimental::propagate_const>>m;};所以您不能只用传播的uniqu

c++ - 带有自定义分配器的 const T 的 STL 容器是否格式错误?

采用以下代码片段:#includestd::vectorgood;//illegal,becausestd::allocatorisill-formedstd::vectorbad;//failstocompileunderMSVS2017std::vectorX;为什么X编译失败?MSVS2017展会ErrorC2338(failedstaticassert):TheC++Standardforbidscontainersofconstelementsbecauseallocatorisill-formed.据我了解,这不一定是正确的。根据20.5.3.5[allocator.req

c++ - std::allocator_traits::construct with const 指针

下面的代码可以正常编译:#include#includeintmain(){constint*a=newint(5);std::cout>;autoalloc=std::allocator();at::construct(alloc,a);std::cout在libstdc++的背后::new((void*)a)int;但是a是const!这是未定义的行为吗?或者placementnew不算修改?我修改了*a的值,是const。据我了解,这是不允许的:Modifyingaconstobjectthroughanon-constaccesspathandreferringtoavolat

c++ - 在 C++ 中,返回指向非常量对象的指针的 const 方法是否被认为是不好的做法?

在C++中,返回指向非const对象的指针的const方法是否被视为不良做法?例如。考虑以下方法://MakesperfectsenseboolisActive()const{returnm_isActive;}//Makesperfectsense,bothconstsensurenothingismodifiedconstSomeObject*someObject()const{returnm_someObject;}//Makesperfectsense,noconstbecauseSomeObjectismutable,//thusMyClassisindirectlymuta

c++ - 我是否需要为右值引用显式地重载接受 const 左值引用的方法?

目前我正在研究右值引用(C++11、g++和gnu++x0),我想在我的类中实现移动语义,因为它感觉“正确”。我是否需要重载通常会接受const左值引用以从右值引用中获益的每个函数?假设这是我的示例类:classPerson{public:Person()=default;Person(std::string&name);Person(constPerson&rhs);Person(Person&&rhs);Person&operator=(constPerson&rhs);Person&operator=(Person&&rhs);std::string&get_name()cons

c++ - 模板变量作为字符串而不是 const char *

我喜欢vector,并且通常在数组上使用它们。出于这个原因,我创建了一个模板化的可变参数函数来初始化vector(包括在下面)。标题(.h):templatevectorinitVector(constintargCount,Tfirst,...);来源(.hpp):templatevectorinitVector(constintargCount,Tfirst,...){vectorretVec;retVec.resize(argCount);if(argCount它适用于大多数类型(例如int、double...),但不适用于字符串——因为编译器将它们解释为“constchar*”

C++11: `auto` 对 const 和引用类型的操作的标准引用

假设我有一个类型T:typedef...T;然后我有这些功能:Tf11();T&f12();T&&f13();constTf21();constT&f22();constT&&f23();然后像这样称呼他们:autox11=f11();autox12=f12();autox13=f13();autox21=f21();autox22=f22();autox23=f23();从C++11标准的哪些部分/条款可以推导出x11..x23的等效非自动声明? 最佳答案 它在§7.1.6.4autospecifier中。在您的函数返回类型示例中

c++ - 遍历 const boost::graph 的边权重

我需要遍历图的边并检查每条边的权重。我没有修改边,因此我的函数采用对图形的常量引用。但是,我知道获得边权重的唯一方法是访问属性映射,这似乎违反了常量性。voidprintEdgeWeights(constGraph&graph){typedefGraph::edge_iteratorEdgeIterator;std::pairedges=boost::edges(graph);typedefboost::property_map::typeWeightMap;//Thefollowinglinewillnotcompile:WeightMapweights=boost::get(boo